package com.huike.contract.service.impl;

import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbRulePool;
import com.huike.clues.mapper.SysUserMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.ISysUserService;
import com.huike.clues.service.ITbRulePoolService;
import com.huike.clues.utils.HuiKeCrmDateUtils;
import com.huike.clues.utils.JobUtils;
import com.huike.common.constant.Constants;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.exception.CustomException;
import com.huike.contract.domain.vo.TransferVo;
import com.huike.contract.service.ITransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TransferServiceImpl implements ITransferService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;

    @Autowired
    private ITbRulePoolService rulePoolService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TbClueMapper clueMapper;


    @Autowired
    private TbBusinessMapper businessMapper;


    @Override
    public List<TransferVo> selectTbClueList(SysUser user) {
        List<TransferVo> transfervoList =new ArrayList<>();
        List<SysUser> list = userService.selectUserList(user);
        for (SysUser sysUser : list) {
            TransferVo transfervo= new TransferVo();
            int clueNum= assignRecordMapper.countAssignCluesByUser(sysUser.getUserId());
            int businessNum= assignRecordMapper.countAssignBusinessByUser(sysUser.getUserId());
            if(clueNum+businessNum>0){
                transfervo.setUserName(sysUser.getUserName());
                transfervo.setClueNum(clueNum);
                transfervo.setBusinessNum(businessNum);
                transfervo.setUserId(sysUser.getUserId());
                transfervo.setPhonenumber(sysUser.getPhonenumber());
                transfervo.setCreateTime(sysUser.getCreateTime());
                transfervoList.add(transfervo);
            }
        }
        return transfervoList;
    }

    @Override
    public void updateAssignment(String type, Long userId, Long transferUserId) {
        //分配规则
        TbRulePool rulePool = rulePoolService.selectTbRulePoolByType(type);

        //线索分配记录
        int userClueAssignRecords = assignRecordMapper.countAssignCluesByUser(userId);
        if (userClueAssignRecords>rulePool.getMaxNunmber()){
            throw new CustomException("转派失败！分配人线索量超过个人最大保有量:" + rulePool.getMaxNunmber());
        }
        //查询接收人是否超过线索池规则
        int transferClue = assignRecordMapper.countAssignCluesByUser(transferUserId);
        if (transferClue>rulePool.getMaxNunmber()){
            throw new CustomException("转派失败！被分配人线索量超过个人最大保有量:" + rulePool.getMaxNunmber());
        }
        int sum=userClueAssignRecords+transferClue;
        long remain=rulePool.getMaxNunmber()-sum>0?sum-rulePool.getMaxNunmber():0;
        if (sum >= rulePool.getMaxNunmber()) {
            throw new CustomException("转派失败！被分配人最大保有量(" + rulePool.getMaxNunmber() + ")，剩余可以转派"
                    + remain + "条线索");
        }


        //查询接收人是否超过商机池规则
        int transferBusiness = assignRecordMapper.countAssignBusinessByUser(transferUserId);
        if (transferBusiness>rulePool.getMaxNunmber()){
            throw new CustomException("转派失败！被分配人线索量超过个人最大保有量:" + rulePool.getMaxNunmber());
        }

        int business = assignRecordMapper.countAssignBusinessByUser(userId);
        if (business>rulePool.getMaxNunmber()){
            throw new CustomException("转派失败！分配人商机量超过个人最大保有量:" + rulePool.getMaxNunmber());
        }

        int sum2=business+transferBusiness;
        long remain2=rulePool.getMaxNunmber()-sum2>0?sum-rulePool.getMaxNunmber():0;

        if (business >= rulePool.getMaxNunmber()) {
            throw new CustomException("转派失败！个人最大保有量(" + rulePool.getMaxNunmber() + ")，剩余可以转派"
                    +remain2 + "条商机");
        }

        SysUser sysUser = sysUserMapper.selectUserById(transferUserId);
        String userName = sysUser.getUserName();
        Long deptId = sysUser.getDeptId();

        assignRecordMapper.updayeByUserId(type,userId,transferUserId,userName,deptId);
    }
}
